package com.briup.estore.common.dao;

import com.briup.estore.common.bean.Shop;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author adam
 * @date 2022/1/12
 */
@Repository
public interface IShopDao extends JpaRepository<Shop, Long> {

    @Modifying
    @Query("update  Shop set salesVolume =?2 where id=?1")
    @Transactional
    void updateSalesVolume(Long shopId, Long salesVolume);

    @Modifying
    @Query("update  Shop set visitVolume =?2 where id=?1")
    @Transactional
    void updateVisitVolume(Long shopId, Long visitVolume);

    List<Shop> findByDiscountOrderBySalesVolumeDesc(boolean discount);

    List<Shop> findByCategoryId(Long categoryId);

    Page<Shop> findByNameContains(String shopName, Pageable pageRequest);


    List<Shop> findByNameContaining(String toUpperCase);

    @Query(value = "select c1.name ,count(s.name) num " +
            "from t_category c right  join t_shop s on s.category_id=c.id right join t_category c1  on c.parent_id=c1.id " +
            "where c1.parent_id is null " +
            "group by c1.name;"  ,nativeQuery = true)
    List<String []> categoryStats();
}
